<template>
  <div>
    <div>
      <TopFilter ref="formPanel" :list="formList" :cols="4" labelWidth="80" @filterChange="formchangeFun" @resetChange="resetChange" />
      <FilterTable
        ref="customerTable"
        columnsRef="partPartInventoryAdjustInventorySearchCustomerPage"
        :columns="columnsList"
        :fetchapi="getTableData"
        :dataSource="dataSource"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columnsList = columns)"
        :height="'200'"
        selectionType="multiple"
        :onRowSelectChange="selectRow"
      ></FilterTable>
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog(1)">取 消</el-button>
      <el-button type="primary" @click="closeDialog(2)">确 认</el-button>
    </div>
  </div>
</template>

<script>
import { getSearchGroupInfoList,savePartGroupInfo } from '@/api/partsmanage/partTypeManage/query';

export default {
  props: ['customerNo'],
  name: 'SearchGroupInfo',
  data() {
    return {
      formList: this.createFormList(),
      columnsList: this.createTableColumns(),
      getTableData: getSearchGroupInfoList,
      selectRowData: {},
      fetchParams: {
        customerCode: this.customerNo,
        xhrAbort: true
      },
      dataSource: [],
      orgList: [],
      regionList: []
    };
  },
  created() {

  },
  mounted() {
  },
  methods: {
    createFormList() {
      return [
        { type: 'INPUT', label: '物料组代码', fieldName: 'materialCode', disabled: false },
        { type: 'INPUT', label: '物料组名称', fieldName: 'materialName', disabled: false },
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '物料组代码', dataIndex: 'materialCode', sorter: true, showOverflowTooltip: true, filter: false, filterType: 'input' },
        { title: '物料组名称', dataIndex: 'materialName', width: 150, showOverflowTooltip: true, sorter: true, filter: false, filterType: 'input' }
      ];
    },
    // 重置
    FormPanelreset() {
      this.$refs.formPanel.RESET_FORM();
    },
    FormPanelSeacher() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    formchangeFun(val) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign({}, this.fetchParams, val, { xhrAbort: false });
    },
    selectRow(row) {
      console.log(row);
      this.selectRowData = row;
    },
    confirmFun() {},
    async closeDialog(param) {
      if (param == 1) {
        this.$emit('close', false, param);
      } else {
        if (!this.selectRowData.length) {
          return this.$message.warning('请选择物料组数据！');
        }
        // if (this.selectRowData[0].materialCode == (await getOwnerCode())) {
        //   return this.$message.warning('不能选择当前物料组数据！');
        // }

        const tempObj = {};
        if (this.selectRowData.length > 0) {
          let str = '';
          this.selectRowData.map((x) => {
            str += x.materialCode + ',';
          });
          // tempObj.materialCode = this.selectRowData[0].materialCode;
          tempObj.materialCode = str
          tempObj.id = this.fetchParams.customerCode;
          const res = await savePartGroupInfo(tempObj);
          this.fetchParams = {...this.fetchParams};
          if (res.data === 300) {
            this.$message({
              type: 'info',
              message: '未保存记录'
            });
            // 关闭加载中动画
            this.loadingFlag = false;
            this.closeDialog(1);
          } else {
            this.$message({
              type: 'info',
              message: '保存成功'
            });
            this.$emit('close');
          }
        }else{
          return this.$notify.warning({ title: '提示信息', message: '请勾选后再进行！' });
        }
        }
    },
    resetChange() {
      this.dataSource = [];
    },
  }
};
</script>

<style lang="less" scoped></style>
